local DailyActFL = require("activity.DailyActFL")
local ActDefine = require("activity.Define")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local Map = require("core.Map")
local CopyDefine = require("copy.Define")
local CopyL = require("copy.CopyL")
local RewardConfig = require("excel.Activity").horseFight
local Grid = require("bag.Grid")
local MailLogicFL = require("mail.MailLogicFL")
local Msg = require("core.Msg")
local Buffer = require("buffer.Buffer")
local HtmlUtil = require("common.HtmlUtil")
local SceneConfig = require("excel.Map").scene
local SceneEvent = require("core.SceneEvent")

HORSE_FIGHT_ROOM = HORSE_FIGHT_ROOM or {}
HORSE_FIGHT_RANK = HORSE_FIGHT_RANK or {}
HORSE_FIGHT_COUNT = HORSE_FIGHT_COUNT or {}
--判断是否可以进入
function checkEnter(human)--, copyID, roomID)
  if not DailyActFL.onCheckActIsRunning(ActDefine.ACT_TYPE_HORSE_FIGHT_ACT) then
    Broadcast.sendBroadcast(human, Lang.DOWN, Lang.COPY_HORSE_FIGHT_NOT_START)  --活动暂未开启
    return false
  end
  return true
end
--发送邮件
function sendMail(human, rank)
  for j = #RewardConfig,1,-1  do
    if rank >= RewardConfig[j].rank then
      local nGridList = {}
      nGridList[1] = Grid.create(nil, RewardConfig[j].reward[1], RewardConfig[j].reward[2], 1)
      MailLogicFL.CreateNewMailForUser(human._id, "铁骑争锋活动奖励", "由于您在铁骑争锋活动中的优异表现，特将奖励发给您", nGridList)
      break
    end
  end
end
--房间结束
function onHorseFightRoomEnd()
  for i=1, #HORSE_FIGHT_ROOM do    
    CopyL.roomFinish(CopyDefine.COPY_HORSE_FIGHT_ID, HORSE_FIGHT_ROOM[i], CopyDefine.COPY_FINISH_ACT_END, "活动结束")
    CopyL.roomClear(CopyDefine.COPY_HORSE_FIGHT_ID, HORSE_FIGHT_ROOM[i])
  end
  HORSE_FIGHT_RANK = {}
  HORSE_FIGHT_ROOM = {}
  if _G.logic_id == SceneConfig[3010].logic_id then
    Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, Lang.ACTIVITY_HORSE_FIGHT_END)
  end
end
function onHorseFightRoomStart()
end
--房间开始
function onHorseFightRoomBegin(copyID, roomID)
  HORSE_FIGHT_ROOM[#HORSE_FIGHT_ROOM + 1] = roomID
  local nActData = DailyActFL.onGetActData(ActDefine.ACT_TYPE_HORSE_FIGHT_ACT)
  if not nActData or nActData.status ~= ActDefine.ACT_STATE_RUNNING then
    HORSE_FIGHT_COUNT[roomID] = 0
    return
  end
  if os.time() - nActData.startTime < 30 then
    SceneEvent.doSceneEvent(Map.map_id_to_scene_id[CopyDefine.HORSE_FIGHT_START_MAP][1], 200)
  end 
  HORSE_FIGHT_COUNT[roomID] = os.time() - nActData.startTime
end
function enterEndArea(human)
  if Map.scene_id_to_map_id[human.scene_id] ~= CopyDefine.HORSE_FIGHT_END_MAP 
    or not Map.is_in_area_id(CopyDefine.HORSE_FIGHT_END_MAP, human.x, human.y, CopyDefine.HORSE_FIGHT_END_AREA) then
    return false
  end
  local isHave = false      
  for j=1, #HORSE_FIGHT_RANK do
    if HORSE_FIGHT_RANK[j] == human._id then
      isHave = true
      break
    end
  end
  if not isHave then
    HORSE_FIGHT_RANK[#HORSE_FIGHT_RANK + 1] = human._id
    if #HORSE_FIGHT_RANK < 6 then
      local str = string.format(Lang.ACTIVITY_HORSE_FIGHT_BRO, HtmlUtil.fontName(human.name, human._id), #HORSE_FIGHT_RANK)
      Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, str)
    end
    local sendMsg = Msg.gc.GC_COPY_HORSE_FIGHT_RANK
    sendMsg.rank = #HORSE_FIGHT_RANK
    Msg.send(sendMsg, human.fd)
    sendMail(human, #HORSE_FIGHT_RANK)   
  end
end
--定时器
function onHorseFightOneSec(copyID, roomID)
  HORSE_FIGHT_COUNT[roomID] = HORSE_FIGHT_COUNT[roomID] + 1
  if HORSE_FIGHT_COUNT[roomID] == 30 then 
    SceneEvent.doSceneEvent(Map.map_id_to_scene_id[CopyDefine.HORSE_FIGHT_START_MAP][1], 210)
    local humanList = CopyL.getCopyHumanList(copyID, roomID)
    for i=1, #humanList do      
      Buffer.add(31022, humanList[i], humanList[i])
    end
    return
  end   
  local humanList = CopyL.getCopyHumanList(copyID, roomID)
  for i=1, #humanList do
    if Map.scene_id_to_map_id[humanList[i].scene_id] == CopyDefine.HORSE_FIGHT_END_MAP 
    and Map.is_in_area_id(CopyDefine.HORSE_FIGHT_END_MAP, humanList[i].x, humanList[i].y, CopyDefine.HORSE_FIGHT_END_AREA) then
      local isHave = false      
      for j=1, #HORSE_FIGHT_RANK do
        if HORSE_FIGHT_RANK[j] == humanList[i]._id then
          isHave = true
          break
        end
      end
      if not isHave then
        HORSE_FIGHT_RANK[#HORSE_FIGHT_RANK + 1] = humanList[i]._id
        if #HORSE_FIGHT_RANK < 6 then
          local str = string.format(Lang.ACTIVITY_HORSE_FIGHT_BRO, HtmlUtil.fontName(humanList[i].name, humanList[i]._id), #HORSE_FIGHT_RANK)
          Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, str)
        end
        local sendMsg = Msg.gc.GC_COPY_HORSE_FIGHT_RANK
        sendMsg.rank = #HORSE_FIGHT_RANK
        Msg.send(sendMsg, humanList[i].fd)
        sendMail(humanList[i], #HORSE_FIGHT_RANK)        
      end
    end
  end
end
--进入副本
function onHorseFightHumanEnterCopy(human, copyID, roomID)
  local sendMsg = Msg.gc.GC_COPY_HORSE_FIGHT_RANK_REWARD
  local cache = {}
  sendMsg.info[0] = #RewardConfig
  for i=1, #RewardConfig do
    sendMsg.info[i].rank = RewardConfig[i].rank
    Grid.create(cache, RewardConfig[i].reward[1], RewardConfig[i].reward[2])
    Grid.makeItem(cache, sendMsg.info[i].reward, 0, human)  
  end
  Msg.send(sendMsg, human.fd)
  Buffer.add(RewardConfig[1].bufID, human, human)
  local nActData = DailyActFL.onGetActData(ActDefine.ACT_TYPE_HORSE_FIGHT_ACT)
  if not nActData or nActData.status ~= ActDefine.ACT_STATE_RUNNING or os.time() - nActData.startTime >= 30 then
    return
  end
  local sendMsg = Msg.gc.GC_COPY_HORSE_FIGHT_START
  sendMsg.remain_time = 30 - (os.time() - nActData.startTime)
  Msg.send(sendMsg, human.fd)
end

DailyActFL.onRegisterCallBack(ActDefine.ACT_TYPE_HORSE_FIGHT_ACT, nil, onHorseFightRoomStart, onHorseFightRoomEnd)
